#import "WSQQViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <Photos/Photos.h>

@interface WSQQViewController ()

@property (nonatomic, strong) UIBezierPath *path;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, strong) UIImageView *signatureImageView;

@property (nonatomic, assign) BOOL userHasSigned; // 是否签名

@end

@implementation WSQQViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSignatureView];
    [self setupSaveButton];
   // [self requestPhotoLibraryPermission];
    // 初始化为 NO，表示用户还未签字
    self.userHasSigned = NO;
}

- (void)setupSignatureView {
    // 创建签字面板视图
    UIView *signatureView = [[UIView alloc] initWithFrame:self.view.bounds];
    signatureView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:signatureView];
    
    // 创建路径和图层
    self.path = [UIBezierPath bezierPath];
    self.shapeLayer = [CAShapeLayer layer];
    self.shapeLayer.lineWidth = 2.0;
    self.shapeLayer.strokeColor = [UIColor blackColor].CGColor;
    self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
    [signatureView.layer addSublayer:self.shapeLayer];
    
    // 创建ImageView用于显示签名
    self.signatureImageView = [[UIImageView alloc] initWithFrame:signatureView.bounds];
    [signatureView addSubview:self.signatureImageView];
    
    // 添加手势识别器
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [signatureView addGestureRecognizer:panGesture];
}

- (void)setupSaveButton {
    // 创建保存按钮
    UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    saveButton.frame = CGRectMake(20, CGRectGetMaxY(self.view.bounds) - 80, 100, 40);
    saveButton.backgroundColor = [UIColor blueColor];
    [saveButton setTitle:@"保存签名" forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(saveSignature) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:saveButton];
}

- (void)handlePan:(UIPanGestureRecognizer *)gesture {
    CGPoint currentPoint = [gesture locationInView:gesture.view];
    
    if (gesture.state == UIGestureRecognizerStateBegan) {
        // 开始新的路径
        [self.path moveToPoint:currentPoint];
        self.userHasSigned = YES; // 用户已经在签字
    } else if (gesture.state == UIGestureRecognizerStateChanged) {
        // 添加线段到路径
        [self.path addLineToPoint:currentPoint];
        self.shapeLayer.path = self.path.CGPath;
    }
}

- (void)saveSignature {
    if (self.userHasSigned) {
        // 生成签名图片
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *signatureImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        // 保存签名图片到相册
        UIImageWriteToSavedPhotosAlbum(signatureImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }else {
        NSLog(@"用户未在画板上签字");
    }
    
    
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        NSLog(@"保存失败：%@", error.localizedDescription);
    } else {
        NSLog(@"保存成功");
        // 清空签名面板
        self.path = [UIBezierPath bezierPath];
        self.shapeLayer.path = self.path.CGPath;
        self.signatureImageView.image = nil;
    }
}



@end
